<!-- login页面 -->
<template>
  <div class="container">
    <div class="box">
      <!-- log区域 -->
      <div class="log">
        <img
          src="@/assets/logo.png"
          alt="logo"
        />
      </div>
      <!-- 表单区域 -->
      <el-form
        class="form"
        :rules="loginRules"
        :model="user"
        ref="login"
      >
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="user.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            type="password"
            v-model="user.password"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button
            type="primary"
            @click="submitLoginForm"
          >登录</el-button>
          <el-button
            type="info"
            @click="resetLoginForm"
          >重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "login",
  data() {
    return {
      user: {
        username: "admin",
        password: "123456",
      },
      // 表单验证规则
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitLoginForm() {
      this.$refs.login.validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$http.post("login", this.user);
          if (res.meta.status !== 200) return this.$message.error("登录失败");
          this.$message.success("登录成功");
          sessionStorage.setItem("token", res.data.token);
          this.$router.push("/home");
        } else {
          this.$message.error("登录不合法");
          return false;
        }
      });
    },
    resetLoginForm() {
      this.$refs.login.resetFields();
    },
  },
};
</script>

<style scoped>
.container {
  height: 100%;
  background-color: #304863;
}

.box {
  width: 450px;
  height: 300px;
  background-color: #ffffff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.log {
  width: 130px;
  height: 130px;
  border: 1px solid #eeeeee;
  border-radius: 50%;
  box-shadow: 0 0 10px #eeeeee;
  padding: 10px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #ffffff;
}

.log img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eeeeee;
}

.btns {
  display: flex;
  justify-content: flex-end;
}

.form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 10px;
  box-sizing: border-box;
}
</style>
